{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b2c558c4-6157-498c-9510-427633c5faa7",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    " \n",
    "# 提取特征和标签\n",
    "train_images, train_labels = next(iter(trainloader))[0].numpy(), next(iter(trainloader))[1].numpy()\n",
    "test_images, test_labels = next(iter(testloader))[0].numpy(), next(iter(testloader))[1].numpy()\n",
    " \n",
    "# 转换为numpy数组\n",
    "train_images = train_images.reshape(train_images.shape[0], -1)  # Flatten images\n",
    "test_images = test_images.reshape(test_images.shape[0], -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "45e0228c-2ad4-43dd-ad27-1806a37dd6ec",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 定义大（≥5）和小（<5）标签\n",
    "train_labels_binary = (train_labels >= 5).astype(int)\n",
    "test_labels_binary = (test_labels >= 5).astype(int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "ec9f7ef5-0b94-4829-9ce7-6be0021b1111",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "from sklearn.model_selection import train_test_split\n",
    " \n",
    "# 划分训练集和验证集\n",
    "X_train, X_val, y_train, y_val = train_test_split(train_images, train_labels_binary, test_size=0.2, random_state=42)\n",
    " \n",
    "# 构建并训练逻辑回归模型\n",
    "log_reg = LogisticRegression()\n",
    "log_reg.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2f20b57b-fd8a-4421-9535-06694b5cf0df",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    " \n",
    "# 使用整个训练集\n",
    "softmax_reg = LogisticRegression(multi_class='multinomial', solver='lbfgs', max_iter=1000)\n",
    "softmax_reg.fit(train_images, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7f10132d-e546-421a-8bd7-dca1ab21c7a1",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.svm import SVC\n",
    " \n",
    "# 划分训练集和验证集（与之前相同）\n",
    "svm_binary = SVC(kernel='linear')\n",
    "svm_binary.fit(X_train, y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1d9b5e2-ff0d-44c1-9cba-b8ffdae5b939",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.multiclass import OneVsRestClassifier\n",
    " \n",
    "svm_ovr = OneVsRestClassifier(SVC(kernel='linear'))\n",
    "svm_ovr.fit(train_images, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "eb996713-71e5-4409-bc09-e2dff4512614",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.multiclass import OneVsOneClassifier\n",
    " \n",
    "svm_ovo = OneVsOneClassifier(SVC(kernel='linear'))\n",
    "svm_ovo.fit(train_images, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f06d23c6-0ca0-44b0-88d5-e4018e0552eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.tree import DecisionTreeClassifier\n",
    " \n",
    "decision_tree = DecisionTreeClassifier()\n",
    "decision_tree.fit(train_images, train_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0043d970-67ba-42b4-a823-52df99a30416",
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.ensemble import RandomForestClassifier\n",
    " \n",
    "# 构建不同子树数量的随机森林模型\n",
    "n_estimators_list = [10, 50, 100, 200]\n",
    "for n_estimators in n_estimators_list:\n",
    "    rf = RandomForestClassifier(n_estimators=n_estimators)\n",
    "    rf.fit(train_images, train_labels)\n",
    "    # 可以添加代码来评估模型性能"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
